﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.ParentForm;
using UI.Lib;

namespace UI.ShopManagement
{
    public partial class FrmBas1a40CEdit : BaseForm
    {
        //控件分组
        private Dictionary<string, string[]> ctls = new Dictionary<string, string[]>();

        public FrmBas1a40CEdit(string _key, string _value)
        {
            InitializeComponent();

            this.Size = base.LstWindow;
            this.key = _key;
            this.value = _value;

            AddCtl();
            SetDefault();

            if (string.IsNullOrEmpty(this.key))
            {
                EnabledCtl("r2"); //默认选中第二项           
            }
            else
            {
                EnabledCtl(this.key);
                SetCheckedValue(this.key, this.value);
            }
        }

        void AddCtl()
        {
            ctls.Add("r1", new string[] { "box1" });
            ctls.Add("r2", null);
            ctls.Add("r3", new string[] { "cb1" });
            ctls.Add("r4", new string[] { "cb2" });
            ctls.Add("r5", new string[] { "cb3" });
            ctls.Add("r6", new string[] { "cb4" });
        }

        void SetDefault()
        {
            this.cb1.SelectedIndex = this.cb2.SelectedIndex = this.cb3.SelectedIndex = this.cb4.SelectedIndex = 0;
        }

        void EnabledCtl(string radioGroupName)
        {
            Control c = null;
            bool isNow = false;
            RadioButton r = null;
            foreach (KeyValuePair<string, string[]> kv in this.ctls)
            {
                isNow = kv.Key == radioGroupName; //是不是当前选中的RadioButton

                ((RadioButton)this.Controls[kv.Key]).Checked = isNow;
                if (kv.Value == null) continue;
                //设置其他控件的启用状态
                foreach (string ctlName in kv.Value)
                {
                    c = this.Controls[ctlName];
                    if (c == null) continue;

                    c.Enabled = isNow;
                }
            }
        }

        private void rd1_Click(object sender, EventArgs e)
        {
            Control c = sender as Control;

            EnabledCtl(c.Name);
        }

        private void tspBtnSave_Click(object sender, EventArgs e)
        {
            GetCheckedValue();
            this.Close();
        }

        private void tspBtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmBas1a40CEdit_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F8:    //按下的是Insert
                    if (CanClickButton(this.tspBtnSave)) tspBtnSave_Click(this.tspBtnSave, null);
                    break;
                case Keys.PageUp:    //按下的是page up，Id号为106 == 返回及关闭
                    this.Close();
                    break;
            }
        }

        void GetCheckedValue()
        {
            RadioButton r = null;
            Control c = null;
            this.key = "";
            this.value = "";
            foreach (KeyValuePair<string, string[]> kv in this.ctls)
            {
                r = this.Controls[kv.Key] as RadioButton;
                if (r == null) continue;

                if (r.Checked == false) continue;

                key = r.Name;

                if (kv.Value == null) continue;
                //设置其他控件的启用状态
                foreach (string ctlName in kv.Value)
                {
                    c = this.Controls[ctlName];
                    if (c == null) continue;

                    if (c is NumericUpDown)
                    {
                        value = ((NumericUpDown)c).Value.ToString();
                    }
                    else if (c is ComboBox)
                    {
                        value = ((ComboBox)c).SelectedIndex.ToString();
                    }
                }
            }
        }

        void SetCheckedValue(string rname, string value)
        {
            Control c = null;
            foreach (KeyValuePair<string, string[]> kv in this.ctls)
            {
                if (rname != kv.Key || kv.Value == null) continue;

                foreach (string ctlName in kv.Value)//设置其他控件的启用状态
                {
                    c = this.Controls[ctlName];
                    if (c == null) continue;

                    if (c is NumericUpDown)
                    {
                        NumericUpDown temp = c as NumericUpDown;
                        temp.Value = Convert.ToDecimal(value);
                    }
                    else if (c is ComboBox)
                    {
                        ComboBox com = c as ComboBox;
                        com.SelectedIndex = Convert.ToInt32(value);
                    }
                }
            }
        }

        public string Key { get { return key; } }
        public string Value { get { return value; } }

        private string key;
        private string value;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.AutoScaleMode = AutoScaleMode.None;
            this.Font = this.tspTop.Font = Global.AllFont;
        }
    }
}
